package com.example.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ScriptExecutor {
    
    public static void main(String[] args) {
        // 示例：执行一个带参数的shell脚本
        String scriptPath = "/path/to/your/script.sh";
        String[] scriptArgs = {"arg1", "arg2", "arg3"};
        
        // 或者执行一个带参数的Python脚本
        String pythonScript = "python3 /path/to/your/script.py";
        String[] pythonArgs = {"param1", "param2", "param3"};
        
        // 执行shell脚本示例
        executeScriptWithArgs(scriptPath, scriptArgs);
        
        // 执行Python脚本示例
        executePythonScript(pythonScript, pythonArgs);
    }
    
    /**
     * 执行带参数的脚本
     * @param scriptPath 脚本路径
     * @param args 参数数组
     */
    public static void executeScriptWithArgs(String scriptPath, String[] args) {
        try {
            // 构建命令
            StringBuilder commandBuilder = new StringBuilder();
            commandBuilder.append("bash ").append(scriptPath);
            
            for (String arg : args) {
                commandBuilder.append(" ").append(arg);
            }
            
            String command = commandBuilder.toString();
            System.out.println("执行命令: " + command);
            
            // 执行命令
            Process process = Runtime.getRuntime().exec(command);
            
            // 读取输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            
            // 读取错误输出
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String errorLine;
            while ((errorLine = errorReader.readLine()) != null) {
                System.err.println("错误: " + errorLine);
            }
            
            // 等待进程完成
            int exitCode = process.waitFor();
            System.out.println("脚本执行完成，退出码: " + exitCode);
            
        } catch (IOException | InterruptedException e) {
            System.err.println("执行脚本时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 执行Python脚本
     * @param scriptCommand 包含Python解释器和脚本路径的命令
     * @param args 参数数组
     */
    public static void executePythonScript(String scriptCommand, String[] args) {
        try {
            // 构建命令
            StringBuilder commandBuilder = new StringBuilder();
            commandBuilder.append(scriptCommand);
            
            for (String arg : args) {
                commandBuilder.append(" ").append(arg);
            }
            
            String command = commandBuilder.toString();
            System.out.println("执行命令: " + command);
            
            // 执行命令
            Process process = Runtime.getRuntime().exec(command);
            
            // 读取输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            
            // 读取错误输出
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String errorLine;
            while ((errorLine = errorReader.readLine()) != null) {
                System.err.println("错误: " + errorLine);
            }
            
            // 等待进程完成
            int exitCode = process.waitFor();
            System.out.println("Python脚本执行完成，退出码: " + exitCode);
            
        } catch (IOException | InterruptedException e) {
            System.err.println("执行Python脚本时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 使用ProcessBuilder执行带参数的脚本（更灵活的方式）
     * @param command 命令列表，第一个元素是脚本路径，后续是参数
     */
    public static void executeScriptWithProcessBuilder(String... command) {
        try {
            ProcessBuilder processBuilder = new ProcessBuilder();
            processBuilder.command(command);
            
            // 可以设置工作目录
            // processBuilder.directory(new File("/path/to/working/directory"));
            
            // 启动进程
            Process process = processBuilder.start();
            
            // 读取输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            
            // 读取错误输出
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String errorLine;
            while ((errorLine = errorReader.readLine()) != null) {
                System.err.println("错误: " + errorLine);
            }
            
            // 等待进程完成
            int exitCode = process.waitFor();
            System.out.println("脚本执行完成，退出码: " + exitCode);
            
        } catch (IOException | InterruptedException e) {
            System.err.println("执行脚本时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}